package org.dr.springmvcdemo.streamAPI;

/**
 * 分组和统计
 * 流式API可以轻松的对数据进行分组和聚合操作
 */

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class GroupingAndCounting {
    public static void main(String[] args) {
        List<String> fruits = Arrays.asList("apple", "banana", "cherry", "apple", "banana");

        // 按水果类型分组并统计数量
        Map<String, Long> result = fruits.stream()
                .collect(Collectors.groupingBy(fruit -> fruit, Collectors.counting()));

        System.out.println("Fruits grouped by type: " + result);
    }
}
